<?php

/*
 * This file will perform the ajax for creating a department.
 * This is being used by admCreateDept.php.
 * 
 * Created by: Peter Agno Jr.
 * Date created: November 22, 2011
 * 
 * In parameters: deptId and deptName
 * Out parameters: flag and msg about the success of creation of department
 */

//********************************************************************************************************
/*
 * Define the functions in here
 */
    // Start - Check if the departmentId is existing
    function idNotExist($deptId) {
        $query =
            "
                SELECT *
                FROM Department
                WHERE departmentId = '$deptId'
            ";
        
        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        
        if ( mysql_num_rows($result) == 0 ) {
            return true;
        }
        else {
            return false;
        }
    } // End - Check if the departmentId is existing
    
    // Start - Check for the duplication of name
    function nameNotExist($deptName) {
        $query =
            "
                SELECT *
                FROM Department
                WHERE departmentName = '$deptName'
            ";
        
        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        
        if ( mysql_num_rows($result) == 0 ) {
            return true;
        }
        else {
            return false;
        }
    } // End - Check for the duplication of name
    
    // Start - Insert data in Department
    function createDept($deptId, $deptName) {
        $query =
            "
                INSERT INTO Department (departmentId, departmentName, departmentLastUpdated) VALUES
                    ('$deptId', '$deptName', NULL);
            ";

        mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    } // End - Insert data in Department
    
//********************************************************************************************************

session_start();

// Start - Checker for those users who will just go to the page by typing directly in the url.
if ($_POST) {
    include('../includes/siteConfig.php');
    
    // connect to database
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
    
    // Get the data from post
    $deptId = $_POST['deptId'];
    $deptName = $_POST['deptName'];
    
    // Check if the departmentId is existing
    if ( idNotExist($deptId) == true ) {
        // Check for the duplication of name
        if ( nameNotExist($deptName) == true ) {
            
            createDept($deptId, $deptName);
            
            $jsondata = array();
            $jsondata['msg'] = $deptName . " department was successfully created.";
            $jsondata['flag'] = "success";
            $feed[] = $jsondata;
            echo json_encode($feed);
        }
        else {
            $jsondata = array();
            $jsondata['msg'] = "Error : Department Name already exists.";
            $jsondata['flag'] = "error";
            $feed[] = $jsondata;
            echo json_encode($feed);
        }
    }
    else {
        $jsondata = array();
        $jsondata['msg'] = "Error : Department Id already exists.";
        $jsondata['flag'] = "error";
        $feed[] = $jsondata;
        echo json_encode($feed);
    }
    
    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
    echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>
